import re
import time


def login():
    '''用户登录和注册界面'''
    RE_USER_INFO = {}
    USER_INFO = {'luoxiang': {'昵称': 'luoxiang', '真实姓名': '罗翔', '身份证号': '533001199107236616', '电话': '13987054232', '邮箱': '634354682@qq.com', '密码': 'luoxiang666666'}}
    PERSONAL_INFO = {}

    #定义用户信息规则
    RE_NICKNAME = re.compile(r'[a-zA-Z0-9]\w+')
    RE_REALNAME = re.compile(r'^[\u4e00-\u9fa5]{2,4}$')
    RE_IDCARD = re.compile(r'^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$')  # 18位身份证对象
    RE_PHONE = re.compile(r'13\d\d{8}|14[5,7,9]\d{8}|15[0,1,2,3,5,6,7,8,9]\d{8}|166\d{8}|17[1,2,3,5,6,7,8]\d{8}|18\d{8}|19[8,9]\d{8}|\d{3}-\d{8}|\d{4}-\d{7}')  # 电话对象
    RE_EMAIL = re.compile(r'^[\da-z]+([\-\.\_]?[\da-z]+)*@[\da-z]+([\-\.]?[\da-z]+)*(\.[a-z]{2,})+$')  # 邮箱对象
    RE_PASSWORD = re.compile(r'^[a-zA-Z]\w{7,17}$')

    # 定义用户信息规则字典
    RE_USER_INFO['昵称'] = RE_NICKNAME
    RE_USER_INFO['真实姓名'] = RE_REALNAME
    RE_USER_INFO['身份证号'] = RE_IDCARD
    RE_USER_INFO['电话'] = RE_PHONE
    RE_USER_INFO['邮箱'] = RE_EMAIL
    RE_USER_INFO['密码'] = RE_PASSWORD
    try:
        print('欢迎使用51备忘录')
        time.sleep(1)
        choice = input("用户登录请按1，实名注册请按2:")
        if choice == '1':
            while True:
                print('用户登录界面'.center(30, '-'))
                login = input('用户昵称:')
                if login in USER_INFO.keys():
                    num = 0
                    while True:
                        password = input('输入密码：')
                        if password == USER_INFO[login]['密码']:
                            print("登录成功！")
                            return False
                        else:
                            num += 1
                            if num == 3:
                                print(f'密码错误{num}次，账号锁定，请查证后再登录')
                                return False
                            else:
                                print(f'密码错误{num}次，请重新输入：')
                            continue
                else:
                    print("输入错误，请重新输入")
                    continue

        if choice == '2':
            print('用户注册界面'.center(30, '-'))
            time.sleep(1)
            print('请按要求填写注册信息(必填)：')
            time.sleep(1)
            print('昵称：必须由26个大小写字母和数字组成')
            time.sleep(1)
            print('真实姓名：汉字，需要和身份证姓名一致')
            time.sleep(1)
            print('身份证号：18位数字组成')
            time.sleep(1)
            print('邮箱：用户本人正在使用的有效邮箱')
            time.sleep(1)
            print('密码：首位必须为字母，密码长度8-18位')
            is_add = True
            while (is_add):
                for key_CH, re_obj in RE_USER_INFO.items():
                    login = input(f'{key_CH}:')
                    result = re_obj.match(login)  
                    if result:
                        PERSONAL_INFO[key_CH] = result.group()
                    else:
                        while True:
                            print('格式错误，请重新输入')
                            login = input(f'{key_CH}:')
                            result = re_obj.match(login)  
                            if result:
                                PERSONAL_INFO[key_CH] = result.group()
                                break
                            else:
                                continue

                if len(PERSONAL_INFO) == 6:
                    # USER_INFO字典用来存储和调用用户信息
                    USER_INFO[PERSONAL_INFO['昵称']] = PERSONAL_INFO
                    print(f'''用户{PERSONAL_INFO['昵称']}注册信息：{PERSONAL_INFO}''')
                    print(f'注册成功！已注册用户{len(USER_INFO)}名:{USER_INFO.keys()}')
                    print('y:继续注册，n：登录备忘录')
                    is_add = input().strip() == 'y'
                    
    except Exception as e:
        print(e)


def main():
    login()

if __name__ == "__main__":
    main()    
